
LyLock_Healthstone = { }
table.insert(LyLock_Plugins, LyLock_Healthstone)
LyLock_Healthstone.Frame = "LyLockHealthstoneFrame"
LyLock_Healthstone.Button = "LyLockHealthstoneButton"
LyLock_Healthstone.Events = {
	"TRADE_SHOW",
    "TRADE_REQUEST",
    "TRADE_CLOSED",
    "TRADE_REQUEST_CANCEL",
    "TRADE_ACCEPT_UPDATE",
    "BAG_UPDATE",
    "BAG_UPDATE_COOLDOWN",
    "PLAYER_LEAVE_COMBAT"
}





------------------------------------------------------
-- Healthstone Settings ------------------------------
------------------------------------------------------

LyLock_Healthstone.OnSettings = function(action)
    if action=="default" then

        LyLock_Settings.Healthstone_Button = ReturnSetting(LyLock_Settings.Healthstone_Button, true)
        LyLock_Settings.Healthstone_Tooltip = ReturnSetting(LyLock_Settings.Healthstone_Tooltip, true)
        LyLock_Settings.Healthstone_UseCombat = ReturnSetting(LyLock_Settings.Healthstone_UseCombat, true)
        LyLock_Settings.Healthstone_HealthCap = ReturnSetting(LyLock_Settings.Healthstone_HealthCap, true)        
        LyLock_Settings.Healthstone_TradeRequest = ReturnSetting(LyLock_Settings.Healthstone_TradeRequest, true)
        LyLock_Settings.Healthstone_TradeCombat = ReturnSetting(LyLock_Settings.Healthstone_TradeCombat, false)
        LyLock_Settings.Healthstone_TradeRaiding = ReturnSetting(LyLock_Settings.Healthstone_TradeRaiding, true)

    elseif action=="save" then

        LyLock_Settings.Healthstone_Button = iif(Healthstone_Button:GetChecked(), true, false);
        LyLock_Settings.Healthstone_Tooltip = iif(Healthstone_Tooltip:GetChecked(), true, false);
        LyLock_Settings.Healthstone_UseCombat = iif(Healthstone_UseCombat:GetChecked(), true, false);
        LyLock_Settings.Healthstone_HealthCap = iif(Healthstone_HealthCap:GetChecked(), true, false);
        LyLock_Settings.Healthstone_TradeRequest = iif(Healthstone_TradeRequest:GetChecked(), true, false);
        LyLock_Settings.Healthstone_TradeCombat = iif(Healthstone_TradeCombat:GetChecked(), true, false);
        LyLock_Settings.Healthstone_TradeRaiding = iif(Healthstone_TradeRaiding:GetChecked(), true, false);
        
    elseif action=="load" then
    
        Healthstone_Button:SetChecked(LyLock_Settings.Healthstone_Button);
        Healthstone_Tooltip:SetChecked(LyLock_Settings.Healthstone_Tooltip);
        Healthstone_UseCombat:SetChecked(LyLock_Settings.Healthstone_UseCombat);
        Healthstone_HealthCap:SetChecked(LyLock_Settings.Healthstone_HealthCap);
        Healthstone_TradeRequest:SetChecked(LyLock_Settings.Healthstone_TradeRequest);
        Healthstone_TradeCombat:SetChecked(LyLock_Settings.Healthstone_TradeCombat);
        Healthstone_TradeRaiding:SetChecked(LyLock_Settings.Healthstone_TradeRaiding);
        
    end
end


--------------------------------------------------------------------------------------
-- Healthstone Functions -------------------------------------------------------------
--------------------------------------------------------------------------------------
LyLock_Healthstone.Started = 0
LyLock_Healthstone.Item = nil
LyLock_Healthstone.Trading = false
LyLock_Healthstone.Placed = false
LyLock_Healthstone.ToolCooldown = ""


LyLock_Healthstone.GetCooldown = function()
    return (120-(GetTime()-LyLock_Healthstone.Started))
end

LyLock_Healthstone.ButtonSetupAction = function(action)
    LyLock_SetupActionStone(LyLockHealthstoneButton, WORD_HEALTHSTONE, WORD_CREATE_HEALTHSTONE, WORD_ITEM_HEALTHSTONE)
end


LyLock_Healthstone.ButtonOnMouseDown = function(action)
    if (LyLock_Healthstone.Item) and (not IsShiftKeyDown()) then
        if (LyLock_Healthstone.Started>0) or (UnitIsDeadOrGhost("player")==1) then
        
            LyLockHealthstoneButton:Disable()
            if (LyLock_Healthstone.Started>0) then
                EchoMessage("LyLock", "Healthstone cooldown is "..strtime( LyLock_Healthstone.GetCooldown() ), "Cooldown")
            else
                EchoMessage("LyLock", "Healthstone is ready to be used", "ReadyUse")
            end
            
        else

            local use = true
            if LyLock_Settings.Healthstone_UseCombat then
                if UnitAffectingCombat("player")==nil then
                    use = false
                    EchoMessage("LyLock", "You must be in combat to use a Healthstone", "Canceled")
                end
            end
        
            if use then
                if LyLock_Settings.Healthstone_HealthCap then
                    local max = UnitHealthMax("player")
                    local cur = UnitHealth("player")
                    if ((max-cur)<=1000) then
                        use = false
                        EchoMessage("LyLock", "Your health is not low enough to use a Healthstone", "Canceled")
                    end
                end
            end
        
            if use then
                LyLockHealthstoneButton:Enable()
            else
                LyLockHealthstoneButton:Disable()
            end

        end
    elseif not IsShiftKeyDown() then
        LyLockHealthstoneButton:Disable()        
        EchoMessage("LyLock", "Your do not have a Healthstone created", "Canceled")    
    else
        LyLockHealthstoneButton:Enable()
    end
end

LyLock_Healthstone.ButtonTooltipText = function()
    if LyLock_Settings.Healthstone_Tooltip then
        return LyLock_Healthstone.ToolCooldown..COLOR_BRIGHTGREEN.."\n".."Click to Use Stone\nShift-Click to Create\n"
    end
end

LyLock_Healthstone.OnSecond = function()
	if LyLock_Healthstone.Trading and (not LyLock_Healthstone.Placed) then
		if (not LyLock_Settings.Healthstone_TradeRaiding) then
			PlaceHealthstoneInTrade()
		elseif (GetNumRaidMembers()>0) or (GetNumPartyMembers()>0) then
			PlaceHealthstoneInTrade()
		end
	end
	

    local cdTime = LyLock_Healthstone.GetCooldown()
    if (LyLock_Healthstone.Started>0) and (cdTime>=0) then
        LyLockHealthstoneCooldownText:SetText(strtimeM(cdTime));
        LyLock_Healthstone.ToolCooldown = COLOR_LIGHTORANGE.."Cooldown: "..COLOR_WHITE..strtimeMin(cdTime).."\n"
    else
        LyLock_Healthstone.Started = 0
        LyLockHealthstoneCooldownText:SetText(iif(LyLock_Healthstone.Item, LYLOCK_FLAGCAST, LYLOCK_FLAGMAKE));
        LyLock_Healthstone.ToolCooldown = COLOR_LIGHTORANGE.."Cooldown: "..COLOR_WHITE.."Ready\n"
    end
    
    
end

LyLock_Healthstone.OnLoad = function()
    LyLock_Healthstone.UpdateItem()
end
LyLock_Healthstone.OnEvent = function()
    if event=="BAG_UPDATE" then
        LyLock_Healthstone.UpdateItem()
    elseif event=="BAG_UPDATE_COOLDOWN" then
        LyLock_Healthstone.UpdateItem()
        
	elseif event=="TRADE_SHOW" or event=="TRADE_REQUEST" then
		if LyLock_Settings.Healthstone_TradeRequest then
			if UnitAffectingCombat("player")==nil then
				LyLock_Healthstone.Placed=false
				LyLock_Healthstone.Trading=true
			elseif not LyLock_Settings.Healthstone_TradeCombat then
				LyLock_Healthstone.Placed=false
				LyLock_Healthstone.Trading=true
			end
		end
	elseif event=="TRADE_CLOSED" or event=="TRADE_REQUEST_CANCEL" then
		LyLock_Healthstone.Trading=false
		LyLock_Healthstone.Placed=false
	elseif event=="TRADE_ACCEPT_UPDATE" then
		if LyLock_Healthstone.Trading and LyLock_Healthstone.Placed then
			AcceptTrade()
		end
	end
	
end
LyLock_Healthstone.UpdateItem = function()
    LyLock_Healthstone.Item = FindContainerItemByName(WORD_HEALTHSTONE)
    if (LyLock_Healthstone.Item) then
        local za, zb, zc = GetContainerItemCooldown(LyLock_Healthstone.Item[1], LyLock_Healthstone.Item[2]);
        LyLock_Healthstone.Started = iif((za>0), za, 0)
    end
end
function PlaceHealthstoneInTrade()
	if LyLock_Healthstone.Item then
		PickupContainerItem(LyLock_Healthstone.Item[1], LyLock_Healthstone.Item[2]);
		ClickTradeButton(1);
		LyLock_Healthstone.Placed=true
	end	
end

